﻿var _helper = new Helper();
var editIndex = undefined;
var ATPs;
var OrderDetailSelected;
var OrderAll;
jQuery(document).ready(function () {

    jQuery('#orderListMenu').addClass('active');
    jQuery('#documentTab').tabs({
        width: 1000,
        border: false,
        onSelect: function (title) {
            switch (title) {
                case "All":
                    break;
                case "Draft":
                    var orderDraft = jQuery.grep(jQuery('#orderAllList').datagrid('getRows'), function (d, i) {
                        return d.Status == 1
                    });

                    jQuery('#draftList').datagrid({
                        data: orderDraft,
                        //loadFilter: pagerFilter,
                        onDblClickRow: function (index, fields, value) {
                            OpenOrderDetailsPopup(fields.OrderNo, fields.Status);
                        }
                    });

                    break;
                case "Wait for approve":
                    var orderWaitForApprove = jQuery.grep(jQuery('#orderAllList').datagrid('getRows'), function (d, i) {
                        return d.Status == 2
                    });

                    jQuery('#orderWaitForApproveList').datagrid({
                        data: orderWaitForApprove,
                        //loadFilter: pagerFilter,
                        onDblClickRow: function (index, fields, value) {
                            OpenOrderDetailsPopup(fields.OrderNo, fields.Status);
                        }
                    });

                    break;
                case "On Process":
                    var orderOnQueue = jQuery.grep(jQuery('#orderAllList').datagrid('getRows'), function (d, i) {
                        return d.Status == 3
                    });

                    jQuery('#orderOnQueueList').datagrid({
                        data: orderOnQueue,
                        //loadFilter: pagerFilter,
                        onDblClickRow: function (index, fields, value) {
                            OpenOrderDetailsPopup(fields.OrderNo, fields.Status);
                        }
                    });
                    break;
                case "Finished":
                    var orderCompleted = jQuery.grep(jQuery('#orderAllList').datagrid('getRows'), function (d, i) {
                        return d.Status == 4
                    });

                    jQuery('#orderCompletedList').datagrid({
                        data: orderCompleted,

                        //loadFilter: pagerFilter,
                        onDblClickRow: function (index, fields, value) {
                            OpenOrderDetailsPopup(fields.OrderNo, fields.Status);
                        }
                    });
                    break;
                case "Rejected":
                    var orderDraft = jQuery.grep(jQuery('#orderAllList').datagrid('getRows'), function (d, i) {
                        return d.Status == 5
                    });

                    jQuery('#rejectedList').datagrid({
                        data: orderDraft,
                        //loadFilter: pagerFilter,
                        onDblClickRow: function (index, fields, value) {
                            OpenOrderDetailsPopup(fields.OrderNo, fields.Status);
                        }
                    });

                    break;
                default:
            }
        }
    });


    jQuery('#orderAllList').datagrid({
        url: "/service/api.ashx?metdhodName=GetOrderAll",
        //loadFilter: pagerFilter,
        //filterBtnIconCls: 'icon-filter',
        onDblClickRow: function (index, fields, value) {
            OpenOrderDetailsPopup(fields.OrderNo, fields.Status);
        },
        onLoadSuccess: function (data) {
            //set default to tab wait for approve
            jQuery('#documentTab').tabs('select', 3)
        }
    });

    jQuery('#orderAllList').datagrid('enableFilter', [

        {
            field: 'TotalQty',
            type: 'numberbox',
            op: ['equal', 'notequal', 'less', 'greater']
        },
        {
            field: 'OrderDate',
            type: 'hidefilter',

        },
        {
            field: 'CreateDate',
            type: 'hidefilter'
        },
        {
            field: 'ApprovedTime',
            type: 'hidefilter'
        },
        {
            field: 'RejectedTime',
            type: 'hidefilter'
        }, ]

    );
});

function newDocumentDetail() {
    jQuery('#orderDetailsPopup').dialog('open');
    jQuery('#newOrderDetailMatno').val('');
    jQuery('#newOrderDetailQty').val('');
}

function SaveDocumentDetail() {
    var document = jQuery('#orderAllList').datagrid('getSelected');
    if (document == null || document == undefined) {
        document = jQuery('#DraftList').datagrid('getSelected');
    }
    if (document == null || document == undefined) {
        document = jQuery('#orderDetailsList').attr("orderno");
    }
    
    var matNo = jQuery('#newOrderDetailMatno').val();
    var qty = jQuery('#newOrderDetailQty').val();
    if (matNo == null || matNo == "" || qty == null || qty == "" || !jQuery.isNumeric(qty)) {
        alert("Please correct the data");
        return;
    }

    _helper.InsertDocumentDetail(document, matNo, qty, function (result) {
        if (result.responseText != '') {
            alert(result.responseText)
        }
        else {
            alert('Insert Success');
        }
        jQuery('#orderDetailsPopup').dialog('close');
    });
}

function exportDocumentDetail() {
    var docno = jQuery('#orderDetailsList').attr("orderno");
    window.open("/service/api.ashx?metdhodName=GetOrderDetailsExport&orderId=" + docno, "_blank", "toolbar=no, scrollbars=no, resizable=yes, top=500, left=500, width=400, height=400");
}
function DeleteDocumentDetail() {
    var document = jQuery('#orderAllList').datagrid('getSelected');
    if (document == null || document == undefined) {
        document = jQuery('#draftList').datagrid('getSelected');
    }
    if (document == null || document == undefined) {
        document = jQuery('#orderWaitForApproveList').datagrid('getSelected');
    }
    var docdetail = jQuery('#orderDetailsList').datagrid('getSelected');
    if (document == null || docdetail == null) {
        alert("Please select document detail to delete");
        return;
    }
    jQuery.messager.confirm('Confirm', 'Are you sure you want to delete Materail No = ' + docdetail.MaterialNo + ')?', function (r) {
        if (r) {
            _helper.DeleteDocumentDetail(document.OrderNo, docdetail.MaterialNo, function (result) {
                alert(result.responseText);
                OpenOrderDetailsPopup(document.OrderNo, document.Status)
            });
        }
    });
}

function saveDocument(status) {
    var document = jQuery('#orderAllList').datagrid('getSelected');

    if (document == null) {
        document = jQuery('#draftList').datagrid('getSelected');
    }

    if (document == null) {
        alert('กรุณาเลือกเอกสารใน Tab All หรือ Tab Draft')
    }

    var documentDetails = jQuery('#orderDetailsList').datagrid('getRows');
    if (status != undefined) {
        document.Status = status;
    }
    _helper.SaveDocument(JSON.stringify(document), JSON.stringify(documentDetails), function (result) {
        alert("Save Success");
        jQuery('#orderDetails').dialog('close');
        jQuery('#orderAllList').datagrid('reload');
    });

}
function SaveComment() {
    var documentDetails = jQuery('#orderDetailsList').datagrid('getRows');
    _helper.SaveComment(JSON.stringify(documentDetails), function (result) {
        alert("Save Success");
        jQuery('#orderDetails').dialog('close');
        jQuery('#orderAllList').datagrid('reload');
    });

}
function deleteDocument() {
    var document = jQuery('#orderAllList').datagrid('getSelected');

    jQuery.messager.confirm('Confirm', 'Are you sure you want to delete order?', function (r) {
        if (r) {
            _helper.DeleteDocument(document.OrderNo, function (result) {
                jQuery('#orderAllList').datagrid('reload');
                alert(result.responseText);
            });
        }
    });


}

function pagerFilter(data) {
    if (typeof data.length == 'number' && typeof data.splice == 'function') {	// is array
        data = {
            total: data.length,
            rows: data
        }
    }
    var dg = jQuery(this);
    var opts = dg.datagrid('options');
    var pager = dg.datagrid('getPager');
    pager.pagination({
        onSelectPage: function (pageNum, pageSize) {
            opts.pageNumber = pageNum;
            opts.pageSize = pageSize;
            pager.pagination('refresh', {
                pageNumber: pageNum,
                pageSize: pageSize
            });
            dg.datagrid('loadData', data);
        }
    });
    if (!data.originalRows) {
        data.originalRows = (data.rows);
    }
    var start = (opts.pageNumber - 1) * parseInt(opts.pageSize);
    var end = start + parseInt(opts.pageSize);
    data.rows = (data.originalRows.slice(start, end));
    return data;
}

function openTransection(docid, itemid, status) {

    var orderSelect = docid;
    if (orderSelect == null || orderSelect == undefined) {
        jQuery('#orderAllList').datagrid('getSelected').OrderNo;
    }
    if (orderSelect == null || orderSelect == undefined) {
        orderSelect = jQuery('#orderOnQueueList').datagrid('getSelected').OrderNo;

    }
    var matSelect = itemid
    openTransectionPopup(orderSelect, matSelect);
}

function OpenOrderDetailsPopup(orderID, Status) {
    jQuery('#orderDetailsList').datagrid({
        url: "/service/api.ashx?metdhodName=GetOrderDetails&orderId=" + orderID + "&status=" + Status,
        pagination: false,
        view: detailview,
        detailFormatter: function (index, row) {
            return '<div style="padding:2px"><table class="ddv"></table></div>';
        },
        onExpandRow: function(index,row){
            var ddv = jQuery(this).datagrid('getRowDetail',index).find('table.ddv');
            ddv.datagrid({
                url: '/service/api.ashx?metdhodName=getOrderConfirm&orderNo=' + row.OrderNo + '&matNo=' + row.ItemId,
                fitColumns:true,
                singleSelect:true,
                rownumbers:true,
                loadMsg:'',
                height:'auto',
                columns:[[
                    {field:'ActualQty',title:'Actual Qty',width:100},
                    {field:'FirmQty',title:'Firm Qty',width:100,align:'right'},
                    {field:'OrderNo',title:'Order No',width:100,align:'right'},
                    {field:'GiQty',title:'Gi Qty',width:100,align:'right'},
                    {field:'GiDate',title:'Gi Date',width:100,align:'right'},
                    {field:'Status',title:'Status',width:150,align:'right'}
                ]],
                onResize:function(){
                    jQuery('#orderDetailsList').datagrid('fixDetailRowHeight',index);
                },
                onLoadSuccess:function(){
                    setTimeout(function(){
                        jQuery('#orderDetailsList').datagrid('fixDetailRowHeight',index);
                    },0);
                }
            });
            //var GiQtySum = 0;
            //for (var i = 0; i < transectionData.length; i++) {
            //    GiQtySum += parseFloat(transectionData[i].Qty);
            //}
            //ddv.datagrid('reloadFooter', [{ GiQty: currentQty }]);
            jQuery('#orderDetailsList').datagrid('fixDetailRowHeight',index);
        },
        onLoadSuccess: function (d,i) {
            if (Status == 1 || Status == 2)
            {
                updateATP(Status);
                jQuery('#OrderDetailDeleteButton').show();
            }
            else if (Status == 3 || Status == 4) {
                jQuery('#OrderDetailDeleteButton').hide();
            }

            //jQuery.each(d.rows, function (i,d) {
            //    _helper.GetConfirmOrder(d.OrderNo, d.ItemId, function (confirmData) {
            //        var status = confirmData;
            //    });
            //});
        },
        onDblClickCell: function (index, field1) {
        var orderSelect = jQuery('#orderAllList').datagrid('getSelected');
        if (orderSelect = null || orderSelect == undefined) {
            orderSelect = jQuery('#orderOnQueueList').datagrid('getSelected');

        }

        if (orderSelect = null || orderSelect == undefined) {
            orderSelect = jQuery('#orderWaitForApproveList').datagrid('getSelected');

        }

        if (orderSelect = null || orderSelect == undefined) {
            orderSelect = jQuery('#orderCompletedList').datagrid('getSelected');

        }


        if (orderSelect != null && orderSelect.Status == 3) {
            var matSelect = jQuery('#orderDetailsList').datagrid('getSelected');
            openTransectionPopup(orderSelect.OrderNo, matSelect.MaterialNo);
        }

        if (editIndex != undefined) {
            //jQuery('#orderDetailsList').datagrid('select', editIndex);
            var t = jQuery('#orderDetailsList'); //My treegrid selector
            t.datagrid('endEdit', editIndex);
            editIndex = undefined;
            return;
        }

        editIndex = index;

        //jQuery(this).datagrid('beginEdit', index, field1);
        jQuery(this).datagrid('selectRow', index).datagrid('editCell', { index: index, field: field1 });
        var ed = jQuery(this).datagrid('getEditor',
                                 { index: index, field: field1 });
        if (ed == null) {
            return;
        }
        jQuery(ed.target).focus().select().bind('keyup', function (e) {
            var code = e.keyCode || e.which;
            if (code == 13) { //Enter keycode
                //Trigger code to save row
                //This executes onAfterEdit event code
                var t = jQuery('#orderDetailsList'); //My treegrid selector
                t.datagrid('endEdit', editIndex, field1);
                editIndex = undefined; //editIndex is a global var

            }
        });
    }
});

//var columnsGrid = jQuery('#orderDetailsList').datagrid('getColumnFields');

switch (Status) {
    case 1:
        jQuery('#UpdateAtpButton').show();
        jQuery('#panelApprover').hide();
        jQuery('#panelSC').show();
        jQuery('#orderDetailsList').datagrid('showColumn', 'MaterialNo');
        jQuery('#orderDetailsList').datagrid('showColumn', 'ATP');
        jQuery('#orderDetailsList').datagrid('showColumn', 'ATPSB');
        jQuery('#orderDetailsList').datagrid('showColumn', 'ATPRY');
        jQuery('#orderDetailsList').datagrid('showColumn', 'ATPTotal');
        jQuery('#orderDetailsList').datagrid('hideColumn', 'OrderNo');
        break;
    case 2:
        jQuery('#UpdateAtpButton').show();
        jQuery('#panelApprover').hide();
        jQuery('#panelSC').hide();
        jQuery('#orderDetailsList').datagrid('showColumn', 'MaterialNo');
        jQuery('#orderDetailsList').datagrid('showColumn', 'ATP');
        jQuery('#orderDetailsList').datagrid('showColumn', 'ATPSB');
        jQuery('#orderDetailsList').datagrid('showColumn', 'ATPRY');
        jQuery('#orderDetailsList').datagrid('showColumn', 'ATPTotal');
        jQuery('#orderDetailsList').datagrid('hideColumn', 'OrderNo');

        break;
    case 3:
        jQuery('#UpdateAtpButton').hide();
        jQuery('#panelApprover').hide();
        jQuery('#panelSC').hide();
        jQuery('#orderDetailsList').datagrid('hideColumn', 'MaterialNo');
        jQuery('#orderDetailsList').datagrid('hideColumn', 'ATP');
        jQuery('#orderDetailsList').datagrid('hideColumn', 'ATPSB');
        jQuery('#orderDetailsList').datagrid('hideColumn', 'ATPRY');
        jQuery('#orderDetailsList').datagrid('hideColumn', 'ATPTotal');
        jQuery('#orderDetailsList').datagrid('showColumn', 'OrderNo');
        break;
    case 4:
        jQuery('#UpdateAtpButton').hide();
        jQuery('#panelApprover').hide();
        jQuery('#panelSC').hide();
        jQuery('#orderDetailsList').datagrid('hideColumn', 'MaterialNo');
        jQuery('#orderDetailsList').datagrid('hideColumn', 'ATP');
        jQuery('#orderDetailsList').datagrid('hideColumn', 'ATPSB');
        jQuery('#orderDetailsList').datagrid('hideColumn', 'ATPRY');
        jQuery('#orderDetailsList').datagrid('hideColumn', 'ATPTotal');
        jQuery('#orderDetailsList').datagrid('showColumn', 'OrderNo');
        break;

    case 5:
        jQuery('#UpdateAtpButton').show();
        jQuery('#panelApprover').hide();
        jQuery('#panelSC').show();
        jQuery('#orderDetailsList').datagrid('hideColumn', 'MaterialNo');
        jQuery('#orderDetailsList').datagrid('hideColumn', 'ATP');
        jQuery('#orderDetailsList').datagrid('hideColumn', 'ATPSB');
        jQuery('#orderDetailsList').datagrid('hideColumn', 'ATPRY');
        jQuery('#orderDetailsList').datagrid('hideColumn', 'ATPTotal');
        jQuery('#orderDetailsList').datagrid('hideColumn', 'OrderNo');
        break;

    default:

}
jQuery('#orderDetailsList').attr("orderno", orderID);
jQuery('#orderDetails').dialog('open').dialog('setTitle', 'รายละเอียดใบสั่งซื้อเลขที่ ' + orderID);
}

function exportTransectionPopup() {
    var docno = jQuery('#transectionDetailsList').attr("orderno");
    var matno = jQuery('#transectionDetailsList').attr("MatNo");
    window.open("/service/api.ashx?metdhodName=GetTransectionByOrderNoAndMatExport&GetTransectionByOrderNoAndMat&orderNo=" + docno + "&matNo=" + matno + "", "_blank", "toolbar=no, scrollbars=no, resizable=yes, top=500, left=500, width=400, height=400");
    
}
function openTransectionPopup(orderNo, MatNo) {
    _helper.GetTransectionByOrderNoAndMat(orderNo, MatNo, function (transectionData) {
        var transectionDetail = jQuery('#transectionDetailsList');
        transectionDetail.attr("orderno", orderNo);
        transectionDetail.attr("MatNo", MatNo);
        var currentQty = 0;
        transectionDetail.datagrid({
            data: transectionData
        });

        for (var i = 0; i < transectionData.length; i++) {
            currentQty += parseFloat(transectionData[i].Qty);
        }

        transectionDetail.datagrid('reloadFooter', [{ Plant: 'Total', Qty: currentQty }]);
        jQuery('#transectionDetails').dialog('open').dialog('setTitle', 'รายละเอียด Transection');
    });

}



function openATPDetailsPopup(matNo, updateIndex, ItemId) {
    if (ATPs == undefined) {
        updateATP();
    }
    var apts;
    for (var key in ATPs) {
        if (key == matNo) {
            apts = ATPs[key];
            break;
        }
    }

    if (apts != undefined) {
        apts = jQuery.grep(apts, function (d) {
            return d.IsApply == 'true';
        });

        jQuery('#AtpDetailsList').datagrid({
            data: apts,
            onDblClickRow: function (i, value) {
                SelectAtp(updateIndex, value);
            }
        });

        jQuery('#selectAtp').linkbutton({
            iconCls: 'icon-save'

        });

        jQuery('#AtpDetails').dialog('open').dialog('setTitle', 'รายละเอียด ATP');

        OrderDetailSelected = updateIndex;
    }
}

function SelectAtp(updateIndex, value) {

    if (updateIndex == undefined) {
        value = jQuery('#AtpDetailsList').datagrid('getSelected');
        updateIndex = OrderDetailSelected;

    }

    if (updateIndex != undefined && value != null) {
        jQuery('#orderDetailsList').datagrid('updateRow', {
            index: updateIndex,
            row: {
                ATP: parseFloat(value.ATPQty),
                Plant: value.Plant
            }
        });
    }
    jQuery('#AtpDetails').dialog('close')
}

function formatRemaining(val, data, index) {
    if (data.IsReviseQtyFromQueue === null || data.IsReviseQtyFromQueue === '') {
        return val;
    }
    else {
        return '<span style="color:red;">' + val + '</span>';
    }
}

function formatPlantChanged(val, data, index) {
    if (data.IsRevisePlantFromQueue === null || data.IsRevisePlantFromQueue === '') {
        return val;
    }
    else {
        return '<span style="color:red;">' + val + '</span>';
    }
}


function formatLinkATP(val, data, index) {
    if (val === null || val === '') {
        return null;
    }
    else {

        //return '<a href=javascript:openATPDetailsPopup("' + data.ItemId + '");>View ATP </a>';
        return "<a href=javascript:openATPDetailsPopup('" + val + "'," + index + ");>View ATP </a>";
    }
}

function formatConfirmStatus(val, d) {
    var status = "unknown";
    if (d.ReceivedQty == 0) {
        status  = "ยังไม่ได้เลย";
    }

    else if (d.ReceivedQty > 0 && d.RemainingQty != 0) {
        status = "ได้สินค้าบางส่วน";
    }

    else if (d.RemainingQty == 0) {
        status = "ได้ของครบ";
    }
    
    var macqty = 0;
    var mbaseqty = 0;
    _helper.GetConfirmOrder(d.OrderNo, d.ItemId, function (confirmData) {
        if (confirmData.length > 0) {
            jQuery.each(confirmData, function (i, d2) {
                macqty += d2.ActualQty;
                mbaseqty += d2.GiQty;
            });
            if ((d.ReceivedQty + d.RemainingQty) - macqty != 0) {
                status = "ออก SO แล้ว";
            }
            if ((d.ReceivedQty + d.RemainingQty) - mbaseqty == 0) {
                status = "GI ครบ";
            }
        }
    });

    return status;
}


function formatLinkTransectionDetail(val, data, index) {
    return "<a href=javascript:openTransection('" + val + "','" + data.ItemId + "','3');>ดู Transection</a>";
}

function formatQty(val) {

    if (val < 0) {
        return '<span style="color:red;">(' + val + ')</span>';
    } else {
        return val;
    }
}

function formatSpecialRate(val,all) {

    if (val != all.DefaultPercent) {
        return '<span style="color:red;">' + val + '</span>';
    } else {
        return val;
    }
}

function formatDelete(val, rec) {
    if (val) {
        return '<span style="color:red;">Delete</span>';
    } else {
        return '<span></span>';
    }
}

function exportDocument() {
    window.open("/service/api.ashx?metdhodName=GetOrderAll&isExport=true", "_blank", "toolbar=no, scrollbars=no, resizable=yes, top=500, left=500, width=400, height=400");
}

function updateATP(status) {
    var documentDetails = jQuery('#orderDetailsList').datagrid('getRows');
    var prodIds = [];
    var orderid;
    jQuery.each(documentDetails, function (i, d) {
        if (orderid == undefined) {
            orderid = d.OrderNo;
        }
        prodIds.push(d.MaterialNo);
    });
    var order = { OrderId: orderid, productIds: prodIds };
    _helper.GetATP(JSON.stringify(order), function (result) {
        ATPs = result;
        jQuery.each(result, function (i, d) {

            var Plant;
            if (status == 2) {
                Plant = new filePlantMaxAtp(d);
               // Plant = new filePlantMaxAtp(d, documentDetails, i);
            }
            else {
                Plant = new filePlantMaxAtp(d);
            }


            var max = Plant.Max;
            var sb = Plant.SumSB;
            var ry = Plant.SumRy;
            var total = Plant.SumTotal;

            for (var i = 0; i < documentDetails.length; i++) {
                if (documentDetails[i].MaterialNo == max.Materialno) {
                    jQuery('#orderDetailsList').datagrid('updateRow', {
                        index: i,
                        row: {
                            ATP: parseFloat(max.ATPQty),
                            Plant: max.Plant,
                            ATPSB: sb,
                            ATPRY: ry,
                            ATPTotal: total
                        }
                    });
                    break;
                }
            };
        });
        var now = new Date();
        var outStr = now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds();
        jQuery('#updateAtpTime').text(outStr);
    });
}

function filePlantMaxAtp(atpAll, docDetails, matid) {

    this.Max = {};
    this.SumSB = 0;
    this.SumRy = 0;
    this.SumTotal = 0;
    atpFilter = atpAll;
    if (docDetails != undefined) {
        var plant = jQuery.grep(docDetails, function (n, i) {
            return n.MaterialNo == matid;
        });
    }

    if (plant != undefined && plant[0].Plant != undefined) {
        atpFilter = atpAll.filter(function (i, o) {
            return i.Plant == plant[0].Plant;
        });
    }

    for (var i = 0; i < atpFilter.length; i++) {
        if ((this.Max.ATPQty == undefined || parseFloat(atpFilter[i].ATPQty) > parseFloat(this.Max.ATPQty)) && atpFilter[i].IsApply == 'true') {
            this.Max = atpFilter[i];
        }
        if (atpFilter[i].Location == 'SB') {
            this.SumSB += parseFloat(atpFilter[i].ATPQty);
        }
        else if (atpFilter[i].Location == 'RY') {
            this.SumRy += parseFloat(atpFilter[i].ATPQty);
        }
        this.SumTotal += parseFloat(atpFilter[i].ATPQty);
    }

}
//function onDoubleClickCell(index, field) {
//    if (endEditing()) {
//        jQuery('#orderDetailsList').datagrid('selectRow', index)
//        .datagrid('editCell', { index: index, field: field });
//        editIndex = index;
//    }
//}

function uploadOrder() {
    window.open("/pages/ImportOrder.aspx", "_blank", "toolbar=no, scrollbars=no, resizable=yes, top=500, left=500, width=400, height=400");
}

jQuery.extend(jQuery.fn.datagrid.methods, {
    editCell: function (jq, param) {
        return jq.each(function () {
            var opts = jQuery(this).datagrid('options');
            var fields = jQuery(this).datagrid('getColumnFields', true).concat(jQuery(this).datagrid('getColumnFields'));
            for (var i = 0; i < fields.length; i++) {
                var col = jQuery(this).datagrid('getColumnOption', fields[i]);
                col.editor1 = col.editor;
                if (fields[i] != param.field) {
                    col.editor = null;
                }
            }
            jQuery(this).datagrid('beginEdit', param.index);
            for (var i = 0; i < fields.length; i++) {
                var col = jQuery(this).datagrid('getColumnOption', fields[i]);
                col.editor = col.editor1;
            }
        });
    }
});

//function endEditing() {
//    if (editIndex == undefined) { return true }
//    if (jQuery('#orderDetailsList').datagrid('validateRow', editIndex)) {
//        jQuery('#orderDetailsList').datagrid('endEdit', editIndex);
//        editIndex = undefined;
//        return true;
//    } else {
//        return false;
//    }
//}